home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / role / Infocom64ToDat.lha / Zip2Dsk.c < prev   
C/C++ Source or Header  |  1993-08-31  |  5KB  |  196 lines

  1. /****************************************************************/
  2. /*    "zip2dsk" converts four Zip-Code files into               */
  3. /*              C-64 disk image                                 */
  4. /*                                                              */
  5. /*    v1.0:   May 16, 1993                                      */
  6. /*                                                              */
  7. /*    Author: Paul David Doherty (h0142kdd@rz.hu-berlin.de)     */
  8. /****************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12.  
  13. #define ZIPNAMELENGTH 16
  14.  
  15. FILE *infile;
  16. FILE *outfile;
  17. char zipname[ZIPNAMELENGTH];
  18.  
  19. int track, max_sect;
  20. unsigned char act_track[21][256];
  21.  
  22. void open_file (int);
  23. void read_track (void);
  24. void read_sector (void);
  25. void write_track (void);
  26. void usage (void);
  27.  
  28. /*******************************************************************/
  29. /*  MAIN function                                                  */
  30. /*******************************************************************/
  31.  
  32. void
  33. main (int argc, char **argv)
  34. {
  35.   char outname[ZIPNAMELENGTH + 4];
  36.  
  37.   if (--argc != 1)
  38.     usage ();
  39.  
  40.   strcpy (zipname, argv[argc]);
  41.   strcpy (outname, zipname);
  42.   strcat (outname, ".dsk");
  43.  
  44.   if ((outfile = fopen (outname, "wb")) == NULL)
  45.     {
  46.       printf ("Error: Can't create \"%s\"\n", outname);
  47.       exit (5);
  48.     }
  49.  
  50.   for (track = 1; track <= 35; track++)
  51.     {
  52.       max_sect = 16 + ((track < 31) ? 1 : 0) + ((track < 25) ? 1 : 0) + ((track < 18) ? 2 : 0);
  53.  
  54.       switch (track)
  55.         {
  56.         case 1:
  57.           open_file (1);
  58.           break;
  59.         case 9:
  60.           open_file (2);
  61.           break;
  62.         case 17:
  63.           open_file (3);
  64.           break;
  65.         case 26:
  66.           open_file (4);
  67.           break;
  68.         }
  69.  
  70.       read_track ();
  71.       write_track ();
  72.     }
  73. }
  74.  
  75.  
  76. /*******************************************************************/
  77. /*  Function: open_file                                            */
  78. /*******************************************************************/
  79.  
  80. void
  81. open_file (int number)
  82. {
  83.   char inputname[ZIPNAMELENGTH + 2];
  84.  
  85.   inputname[0] = number + 0x30;
  86.   inputname[1] = '\0';
  87.   strcat (inputname, "!");
  88.   strcat (inputname, zipname);
  89.  
  90.   if (number > 1)
  91.     fclose (infile);
  92.  
  93.   if ((infile = fopen (inputname, "rb")) == NULL)
  94.     {
  95.       printf ("Error: Can't find \"%s\"\n", inputname);
  96.       exit (5);
  97.     }
  98.  
  99.   fseek (infile, ((number == 1) ? 4 : 2), SEEK_SET);
  100. }
  101.  
  102. /*******************************************************************/
  103. /*  Function: read_track                                           */
  104. /*******************************************************************/
  105.  
  106. void
  107. read_track (void)
  108. {
  109.   int sect;
  110.  
  111.   for (sect = 0; sect <= max_sect; sect++)
  112.     read_sector ();
  113. }
  114.  
  115. /*******************************************************************/
  116. /*  Function: read_sector                                          */
  117. /*******************************************************************/
  118.  
  119. void
  120. read_sector ()
  121. {
  122.   unsigned char trk, sec, len, rep, repnum, chra;
  123.   int i, j, count;
  124.  
  125.   trk = getc (infile);
  126.   sec = getc (infile);
  127.  
  128.   if ((trk & 0x80) != 0)
  129.     {
  130.       len = getc (infile);
  131.       rep = getc (infile);
  132.       count = 0;
  133.  
  134.       for (i = 0; i < len; i++)
  135.         {
  136.           chra = getc (infile);
  137.           if (chra != rep)
  138.             {
  139.               act_track[sec][count] = chra;
  140.               count++;
  141.             }
  142.           else
  143.             {
  144.               repnum = getc (infile);
  145.               chra = getc (infile);
  146.               i = i + 2;
  147.               for (j = 0; j < repnum; j++)
  148.                 {
  149.                   act_track[sec][count] = chra;
  150.                   count++;
  151.                 }
  152.             }
  153.         }
  154.     }
  155.  
  156.   else if ((trk & 0x40) != 0)
  157.     {
  158.       chra = getc (infile);
  159.       for (i = 0; i < 256; i++)
  160.         act_track[sec][i] = chra;
  161.     }
  162.  
  163.   else
  164.     {
  165.       for (i = 0; i < 256; i++)
  166.         act_track[sec][i] = getc (infile);
  167.     }
  168. }
  169.  
  170. /*******************************************************************/
  171. /*  Function: write_track                                          */
  172. /*******************************************************************/
  173.  
  174. void
  175. write_track (void)
  176. {
  177.   int i, j;
  178.  
  179.   for (i = 0; i <= max_sect; i++)
  180.     for (j = 0; j < 256; j++)
  181.       putc (act_track[i][j], outfile);
  182. }
  183.  
  184. /*******************************************************************/
  185. /*  Function: usage                                                */
  186. /*******************************************************************/
  187.  
  188. void
  189. usage (void)
  190. {
  191.   printf ("\nZip To Dsk V1.0\n");
  192.   printf ("\n");
  193.   printf (" Usage: Zip2Dsk Zip_Image_Name\n\n");
  194.   exit (0);
  195. }
  196.